﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('MyNoticeMain.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('MyNoticeMain.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
        >删除</a-button
      >
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <!-- <a-select-option key="Operator">操作人</a-select-option>
              -->
                <a-select-option key="CustomerName">客户名称</a-select-option>
                <a-select-option key="DocumentNo">单据号</a-select-option>

                <a-select-option key="LinkManTel">联系人电话</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
      ><template slot="DeliveryStatus" slot-scope="text">
        <a-tag color="green" v-if="text == 1"> 已发货 </a-tag>
        <a-tag color="blue" v-if="!text">
          未发货
        </a-tag>
      </template>
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <!-- <a v-if="!record.DeliveryStatus" @click="confirmTheDelivery(record.Id)">确认发货</a>
          <a-divider v-if="!record.DeliveryStatus" type="vertical" /> -->
          <a v-if="record.DeliveryStatus" @click="Check(record.Id)">查看</a>
          <!-- <a-divider v-if="!record.DeliveryStatus" type="vertical" /> -->
          <a v-if="hasPerm('MyNoticeMain.Edit') && !record.DeliveryStatus" @click="handleEdit(record.Id)">编辑</a>
          <a-divider
            v-if="hasPerm('MyNoticeMain.Edit') && !record.DeliveryStatus && hasPerm('MyNoticeMain.Delete')"
            type="vertical"
          />
          <a v-if="hasPerm('MyNoticeMain.Delete') && !record.DeliveryStatus" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import moment from 'moment'
const columns = [
  // { title: '单号', dataIndex: 'OddNumbers' },
  // { title: '单据类型', dataIndex: 'BillType' },
  { title: '客户名称', dataIndex: 'CustomerName', width: '15%' },
  // { title: '业务类型', dataIndex: 'SalesType' },
  // { title: '收发类别', dataIndex: 'SendReceiveType' },
  // { title: '组织机构', dataIndex: 'Department' },
  // { title: '业务员', dataIndex: 'Salesman' },
  {
    title: '单据日期',
    dataIndex: 'DocumDate',
    align: 'center',
    customRender: value => {
      if (value) return value.substring(0, 10)
    }
  },
  // { title: '销售发票号', dataIndex: 'SalInvNo' },
  { title: '订单号', dataIndex: 'DocumentNo' },

  // { title: '客户编码', dataIndex: 'cCusCode' },
  // { title: '付款条件编码', dataIndex: 'PayTerCode' },
  { title: '发运方式', dataIndex: 'ShipVia' },

  { title: '收货人', dataIndex: 'ConsigneeName' },

  // { title: '操作人', dataIndex: 'Operator' },
  // { title: '联系人', dataIndex: 'Contacts' },
  // { title: '省区', dataIndex: 'Province' },
  // { title: '城市', dataIndex: 'City' },
  // { title: '地区', dataIndex: 'County' },
  { title: '联系人电话', dataIndex: 'LinkManTel' },
  // { title: '开票名称', dataIndex: 'BillingName' },
  { title: '收货地址', dataIndex: 'Address', width: '15%', ellipsis: true },
  { title: '发货状态', dataIndex: 'DeliveryStatus', scopedSlots: { customRender: 'DeliveryStatus' } },

  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '12%' }
]

export default {
  components: {
    EditForm
  },
  mounted() {
    this.getDataList()
  },
  data() {
    return {
      disabled: false,
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: { condition: 'CustomerName' },
      selectedRowKeys: []
    }
  },
  activated() {
    if (this.$route.query.name) {
      this.queryParam.condition = 'CustomerName'
      this.queryParam.keyword = this.$route.query.name
      this.getDataList()
    } else {
      this.queryParam.condition = 'CustomerName'
      this.queryParam.keyword = ''
      this.getDataList()
    }
  },
  methods: {
    repossess() {
      this.queryParam = { condition: 'CustomerName' }
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    Check(id) {
      this.disabled = true
      this.$refs.editForm.openForm(id, '查看')
    },

    getDataList() {
      this.selectedRowKeys = []

      this.loading = true

      this.$http
        .post('/DeliveryManagement/DEL_DeliveryNoticeMain/GetDataListByMySelfAndMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            //console.log(resJson.Total)
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    confirmTheDelivery(id) {
      var thisObj = this
      this.$confirm('确定发货吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http
            .post('/DeliveryManagement/DEL_DeliveryNoticeMain/UpdateDataByCondition', {
              conditions: [
                {
                  Condition: 'DeliveryStatus',
                  Keyword: '1'
                }
              ],
              ids: [id]
            })

            .then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
        })
      })
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    handleAdd() {
      this.disabled = false
      this.$refs.editForm.openForm(null, '新建发货通知单信息', this.$route.query.id, this.$route.query.name)
    },
    handleEdit(id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '编辑发货通知单信息')
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http.post('/DeliveryManagement/DEL_DeliveryNoticeMain/DeleteDataOfAll', ids).then(resJson => {
            resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
        })
      })
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
